<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/10
 * Time: 10:37
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Teacher extends Base {

    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.user_username|a.user_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($sex)&&$sex!="")
            {
                $map['user_gender'] = $sex;
            }
            if(isset($grade)&&$grade!="")
            {
                $map['user_grade'] = $grade;
            }
            if(isset($subject)&&$subject!="")
            {
                $map['us.subject_id'] = $subject;
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.user_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.user_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.user_time'] = ['between time',[$start,$end]];
            }
            if(isset($user_status)&&$user_status!="")
            {
                $map['a.user_status'] = $user_status;
            }else{
                $map['a.user_status'] =array('neq',5);
            }
            if(isset($userid)&&trim($userid)!=''){
                $map['userid'] = trim($userid);
            }

            $map['a.user_type'] =2;
            
//            $map['a.user_status'] =  array('neq',3);
            $count = Db::table('up_u_user')->alias('a')
                ->join('up_area ua','ua.district_id = a.user_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.user_shi','left')
                ->join('up_area ua2','ua2.district_id = a.user_qu','left')
                ->join('up_grade ug','ug.grade_id = a.user_grade','left')
                ->join('up_subject us','us.subject_id = a.user_subject','left')
                ->join('up_edu', 'a.user_edu_id = edu_id', 'left')
                ->where($map)
                ->count();
            $list = Db::table('up_u_user')->alias('a')
                ->join('up_area ua','ua.district_id = a.user_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.user_shi','left')
                ->join('up_area ua2','ua2.district_id = a.user_qu','left')
                ->join('up_grade ug','ug.grade_id = a.user_grade','left')
                ->join('up_subject us','us.subject_id = a.user_subject','left')
                ->join('up_edu', 'a.user_edu_id = edu_id', 'left')
                ->where($map)
                ->field('a.*,ua.district,ua1.district as district1,
                ua2.district as district2,ug.grade_name,us.subject_name,edu_name')
                ->page($page, $limit)
                ->order('a.user_is_top asc, a.user_id desc')
                ->select();//计算总页面
//            foreach ($list as $key=>&$vo){
//                $vo['address'] = $vo['district'].$vo['district1'].$vo['district2'];
//                $vo['star'] = $this->teacherStar($vo['user_id']);
//            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        $grade = Db::table('up_grade')->select();
        $subject = Db::table('up_subject')->order('subject_sort asc')->select();
        $this->assign('grade',$grade);
        $this->assign('subject',$subject);
        return $this->fetch();
    }

    //删除
    public function del(){
        $id = input('param.id');
        $res = Db::table('up_u_user')->where('user_id',$id)->setField('user_status',5);
        if($res ){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    /***
     * 冻结
     */
    public function frozen(){
        $ids = input('id');
        $res = Db::table('up_u_user')->where('user_id', $ids)->setField('user_status', 4);
        if($res){
            return json(['code' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /***
     * 解冻
     */
    public function thaw(){
        $ids = input('id');
        $res = Db::table('up_u_user')->where('user_id',  $ids)->setField('user_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    //详情
    public function detail(){
        $map['user_id'] = input('param.user_id');
        $list = Db::table('up_u_user')->alias('a')
            ->join('up_area ua','ua.district_id = a.user_sheng','left')
            ->join('up_area ua1','ua1.district_id = a.user_shi','left')
            ->join('up_area ua2','ua2.district_id = a.user_qu','left')
            ->join('up_grade ug','ug.grade_id = a.user_grade','left')
            ->join('up_subject us','us.subject_id = a.user_subject','left')
            ->join('up_teach_style uts','uts.style_id =a.user_tcahe_style','left')
            ->join('up_edu', 'a.user_edu_id = edu_id', 'left')
            ->where($map)
            ->field('a.*,ua.district,ua1.district as district1,
                ua2.district as district2,ug.grade_name,us.subject_name,uts.style_name, edu_name')
            ->find();//计算总页面
            $list['address'] = $list['district'].$list['district1'].$list['district2'];
            $list['star'] = $this->teacherStar($list['user_id']);
        if($list['user_gender']==1){
            $list['user_gender_name'] = '男';
        }else{
            $list['user_gender_name'] = '女';
        }
        if($list['user_honor_img']){
            $list['user_honor_img'] = explode(',', trim($list['user_honor_img'], ','));
        }else{
            $list['user_honor_img'] = [];
        }
        $this->assign('list',$list);
        return $this->fetch();
    }
    
    public function add(){
        if(Request::instance()->isPost()){
            $username = input('username');
            $phone= input('phone');
            $name = input('name');
            $data = [
                'name' => $name,
                'user_phone' => $phone,
                'user_username' =>$username,
                'user_type' =>2,
                'user_time'=>date('Y-m-d H:i:s',time())
            ];
            $res = Db::table('up_u_user')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    
    /**
     * 打款
     * @return mixed|\think\response\Json
     */
    public function dk(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $money = input('param.money');
            $res = Db::table('up_u_user')->where('user_id',$id)->setInc('user_balance',$money);
            $data['dk_user_id'] = session('uid');
            $data['dk_money'] = $money;
            $data['dk_time'] = date('Y-m-d H:i:s',time());
            $data['dk_status'] = 1;
            $data['user_id'] =$id;
            $data['dk_type'] =1;
            $data['dk_type_sex'] =2;
            Db::table('up_dk')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.user_id');
        $this->assign('id',$id);
        return $this->fetch();
    }
    
    /**
     * 扣款
     * @return mixed|\think\response\Json
     */
    public function kk(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $money = input('param.money');
            $user_balance = Db::table('up_u_user')->where('user_id',$id)->value('user_balance');
            if($money>$user_balance){
                return json(['code' => 100, 'msg' => '余额不足,请重新输入']);
            }
            $res = Db::table('up_u_user')->where('user_id',$id)->setDec('user_balance',$money);
            $data['dk_user_id'] = session('uid');
            $data['dk_money'] = $money;
            $data['dk_time'] = date('Y-m-d H:i:s',time());
            $data['dk_status'] = 2;
            $data['user_id'] =$id;
            $data['dk_type'] =2;
            $data['dk_type_sex'] =2;
            Db::table('up_dk')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.user_id');
        $this->assign('id',$id);
        return $this->fetch();
    }
    
    /**
     * 老师置顶
     * @return \think\response\Json
     */
    public function setUserTop(){
        $user_id = input('id');
        $is_top = Db::table('up_u_user')->where('user_id', $user_id)->value('user_is_top');
        if($is_top == 1){
            $user_is_top = 2;
        }else{
            $user_is_top = 1;
        }
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_is_top', $user_is_top);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 邀请的老师
     * @return mixed|\think\response\Json
     */
    public function inviteTeacher(){
        if(request()->isAjax()){
            $user_invite_code = Db::table('up_u_user')->where('user_id', session('user_id'))->value('user_my_code');
            if(empty($user_invite_code)){
                $count = 0;
                $list = [];
            }else{
                $page = input('page') ? input('page'):1;
                $limit = input("limit")?input("limit"):10;// 获取总条数;
                $count = Db::table('up_u_user')
                    ->where('user_invite_code', $user_invite_code)
//                ->where('user_invite_code', 'NEQ', '')
                    ->count();
                $list = Db::table('up_u_user')
                    ->where('user_invite_code', $user_invite_code)
//                ->where('user_invite_code', 'NEQ', '')
                    ->field('user_username, user_phone, user_time, user_status')
                    ->order('user_time desc')
                    ->page($page, $limit)
                    ->select();//计算总页面
            }
            
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        $user_id = input('user_id');
        session('user_id', $user_id);
        return $this->fetch('teacher/inviteteacher');
    }
    
    /**
     * 邀请的收益
     * @return mixed|\think\response\Json
     */
    public function inviteEar(){
        if(request()->isAjax()){
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_ear')
                ->where('ear_invite_user_id', session('user_id'))
                ->join('up_u_user', 'user_id = ear_by_invite_user_id', 'left')
                ->count();
            $list = Db::table('up_ear')
                ->where('ear_invite_user_id', session('user_id'))
                ->join('up_u_user', 'user_id = ear_by_invite_user_id', 'left')
                ->field('user_username, ear_num, ear_time')
                ->order('ear_time desc')
                ->page($page, $limit)
                ->select();//计算总页面
        
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        $user_id = input('user_id');
        session('user_id', $user_id);
        return $this->fetch('teacher/inviteear');
    }
}